package com.yuanxing.oushangyidali.ui

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import androidx.fragment.app.Fragment
import com.fhx.core.base.BaseActivity
import com.fhx.core.permission.OnPermission
import com.fhx.core.utils.ToastUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.yuanxing.oushangyidali.R
import com.yuanxing.oushangyidali.ui.adapter.FragmentAdapter
import com.yuanxing.oushangyidali.ui.billing.BillingFragment
import com.yuanxing.oushangyidali.ui.home.HomeFragment
import com.yuanxing.oushangyidali.ui.me.MeFragment
import com.yuanxing.oushangyidali.ui.search.SearchFragment
import com.yzq.zxinglibrary.common.Constant
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : BaseActivity() {
    //上次点击back键的时间
    private var mLastClickBackTime: Long = 0
    private val homeFragment by lazy { HomeFragment() }
    private val searchFragment by lazy { SearchFragment() }
    private val billingFragment by lazy { BillingFragment() }
    private val meFragment by lazy { MeFragment() }
    val REQUEST_CODE_SCAN = 0X01
    override fun getLayoutId(): Int = R.layout.activity_main
    override fun main() {
        super.main()
        initPage()
        tv_search.setOnClickListener { v ->
            main_view_pager.currentItem = 1
        }
        tv_billing.setOnClickListener { v ->
            main_view_pager.currentItem = 3
        }
        img_saoma.setOnClickListener {
            requestPermission()
        }
    }

    private fun initPage() {
        val fragments = arrayListOf<Fragment>()
        fragments.add(homeFragment)
        fragments.add(searchFragment)
        fragments.add(billingFragment)
        fragments.add(meFragment)
        main_view_pager.currentItem = 0
        main_view_pager.adapter = FragmentAdapter(supportFragmentManager, fragments)
        main_view_pager.offscreenPageLimit = 3
        main_tab_group.setViewPager(main_view_pager)


    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        main_tab_group.resetState()
        main_view_pager.currentItem = 0

    }

    override fun onBackPressed() {
        val curTime = System.currentTimeMillis()
        if (curTime - mLastClickBackTime > 2000) {
            mLastClickBackTime = curTime
            ToastUtils.show(this, getString(R.string.try_again_exit))
            return
        }
        super.onBackPressed()
    }


    private fun requestPermission() {

        XXPermissions.with(this)
            .permission(Permission.CAMERA)
//            .permission(Permission.Group.STORAGE)
            .request(object : OnPermission, OnPermissionCallback {
                override fun hasPermission(granted: MutableList<String>?, all: Boolean) {
                    startActivityForResult(
                        Intent(
                            mContext,
                            com.yzq.zxinglibrary.android.CaptureActivity::class.java
                        ), REQUEST_CODE_SCAN
                    )
                }

                override fun noPermission(denied: MutableList<String>?, quick: Boolean) {
                    if (quick) {
                        ToastUtils.show("授权失败，请手动授予权限")
//                        XXPermissions.startPermissionActivity(this@MainActivity, false)
                    } else {
                        ToastUtils.show("请先授予权限")
                    }
                }

                override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                    if (all) {
                        startActivityForResult(
                            Intent(
                                mContext,
                                com.yzq.zxinglibrary.android.CaptureActivity::class.java
                            ), REQUEST_CODE_SCAN
                        )
                    }
                }

            })
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && data != null) {
            when (requestCode) {
                REQUEST_CODE_SCAN -> {
                    val result = data.getStringExtra(Constant.CODED_CONTENT)
                    if (result != null) {
                        ToastUtils.show(result)
                    }
                }
            }
        }
    }
}
